import { defineStore } from 'pinia';
import router from '../router';
const modules = import.meta.glob([
    '/src/**/**.vue',
])
const layoutModules = import.meta.glob([
    '/src/views/**/**.vue',
])

// 动态加载组件
const loadLayoutComponent = (component) => {
    if (component) {
        return layoutModules[`/src/views${component}/index.vue`]
    }
}
const loadComponent = (component) => {
    if (component) {
        return modules[`/src${component}/index.vue`]
    }
}

export const menuStore = defineStore('menu', {
    state: () => ({

        /*  menuList:[
        
             {
                 name: '主页',
                 path: '/index',
                 component: () => import('@/views/Index/index.vue'),
             },
 
             {
                 name: 'productManagement',
                 path: '/productManagement',
                 children: [
                     {
                         name: '产品类型',
                         path: '/productManagement/producttypemgr',
                         component: () => import('@/views/ProductManagement/producttypemgr/index.vue'),
                     },
                     {
                         name: '产品型号',
                         path: '/productManagement/productmodelmgr',
                         component: () => import('@/views/ProductManagement/productmodelmgr/index.vue'),
                     },
                     {
                         name: '生产批次',
                         path: '/productManagement/productbatchmgr',
                         component: () => import('@/views/ProductManagement/ProductBatchmgr/index.vue'),
                     },
                     {
                         name: '产品记录',
                         path: '/productManagement/productmgr',
                         component: () => import('@/views/ProductManagement/Productmgr/index.vue'),
                     },
                     {
                         name: '老化记录',
                         path: '/productManagement/agerecordmgr',
                         component: () => import('@/views/ProductManagement/Agerecordmgr/index.vue'),
                     },
                     {
                         name: '检测记录',
                         path: '/productManagement/checkrecordmgr',
                         component: () => import('@/views/ProductManagement/CheckRecordmgr/index.vue'),
                     },
                     {
                         name: '托盘记录',
                         path: '/productManagement/tuopanrecordmgr',
                         component: () => import('@/views/ProductManagement/TuopanRecordmgr/index.vue'),
                     },
                     {
                         name: '自动烧录记录',
                         path: '/productManagement/autoburnrecordmgr',
                         component: () => import('@/views/ProductManagement/AutoBurnRecordmgr/index.vue'),
                     },
                 ]
             },
             {
                 name: '统计管理',
                 path: '/statisticalManagement',
                 children: [
                     {
                         name: '老化完成率统计',
                         path: '/statisticalManagement/agepassstatistics',
                         component: () => import('@/views/StatisticalManagement/AgepassStatistics/index.vue'),
                     },
                     {
                         name: '检测合格率统计',
                         path: '/statisticalManagement/checkpassstatistics',
                         component: () => import('@/views/StatisticalManagement/CheckpassStatistics/index.vue'),
                     },
                     {
                         name: '打包出库率统计',
                         path: '/statisticalManagement/packagestatistics',
                         component: () => import('@/views/StatisticalManagement/PackageStatistics/index.vue'),
                     },
                     {
                         name: '产品故障统计',
                         path: '/statisticalManagement/faultstatistics',
                         component: () => import('@/views/StatisticalManagement/FaultStatistics/index.vue'),
                     },
                     {
                         name: '一次直通率统计',
                         path: '/statisticalManagement/firstpassstatistics',
                         component: () => import('@/views/StatisticalManagement/FirstpassStatistics/index.vue'),
                     },
                 ]
             },
             {
                 name: '设备管理',
                 path: '/deviceManagement',
                 children: [
                     {
                         name: '串口服务器',
                         path: '/deviceManagement/serialsvrmgr',
                         component: () => import('@/views/DeviceManagement/Serialsvrmgr/index.vue'),
                     },
                     {
                         name: '工作区域',
                         path: '/deviceManagement/workareamgr',
                         component: () => import('@/views/DeviceManagement/Workareamgr/index.vue'),
                     },
                     {
                         name: '老化车',
                         path: '/deviceManagement/laohuachemgr',
                         component: () => import('@/views/DeviceManagement/Laohuachemgr/index.vue'),
                     },
                     {
                         name: '供电接口',
                         path: '/deviceManagement/powerinterfacemgr',
                         component: () => import('@/views/DeviceManagement/PowerInterfacemgr/index.vue'),
                     },
                 ]
             },
             {
                 name: '系统管理',
                 path: '/systemManagement',
                 children: [
                     {
                         name: '部门管理',
                         path: '/systemManagement/deptmgr',
                         component: () => import('@/views/SystemManagement/Deptmgr/index.vue'),
                     },
                     {
                         name: '用户管理',
                         path: '/systemManagement/usermgr',
                         component: () => import('@/views/SystemManagement/Usermgr/index.vue'),
                     },
                     {
                         name: '角色管理',
                         path: '/systemManagement/rolemgr',
                         component: () => import('@/views/SystemManagement/Rolemgr/index.vue'),
                     },
                     {
                         name: '日志管理',
                         path: '/systemManagement/syslogmgr',
                         component: () => import('@/views/SystemManagement/Syslogmgr/index.vue'),
                     },
                 ]
             }
 
         ] */
        // 路由
        menuList: JSON.parse(localStorage.getItem('MgrModules')) ? JSON.parse(localStorage.getItem('MgrModules')) : [],
        // 实时监控路由
        realTimeMenu: [],
        // 菜单路由
        userMenu: [],


    }),
    actions: {
        SETMENULIST() {
            if (!this.menuList) {
                return
            }
            let mapList = this.menuList.map(item => {
                let name = item.ModuleName
                let path = item.ModuleUrl
                let children
                if (item.Childs) {
                    // 如果是实时监控,就加载实时监控组件的路径
                    if (item.ModuleUrl === '/realTimeMonitoring') {
                        let _children = item.Childs
                        children = _children.map(i => {
                            let name = i.ModuleName
                            let path = i.ModuleUrl
                            return {
                                name,
                                path,
                                component: loadComponent(path)
                            }
                        })
                    } else {
                        let _children = item.Childs
                        children = _children.map(i => {
                            let name = i.ModuleName
                            let path = i.ModuleUrl
                            let meta = {}
                            meta.RolePower = i.RolePower
                            return {
                                name,
                                path,
                                component: loadLayoutComponent(path),
                                meta
                            }

                        })
                    }
                    if (item.ModuleUrl === '/productManagement') {
                        children.push({
                            name: '生产批次型号',
                            path: '/productManagement/productbatchmodel',
                            component: loadLayoutComponent('/productManagement/productbatchmodel'),
                            meta: {
                                hidden: true
                            }
                        })
                    }
                }
                return {
                    name,
                    path,
                    children
                }
            })
            let realTimeMenu = mapList.filter(m => {
                return m.path.includes("/realTimeMonitoring")
            })
            if (realTimeMenu[0]) {
                let _realTimeMenuObj = realTimeMenu[0]
                _realTimeMenuObj.component = loadComponent(_realTimeMenuObj.path)
                //router.addRoute(realTimeMenu[0])
            }
            this.realTimeMenu = realTimeMenu
            let userMenu = mapList.filter(m => {
                return !m.path.includes("/realTimeMonitoring")
            })
            this.userMenu = userMenu
            userMenu.forEach(menu => {
                router.addRoute('layout', menu)
            })
        },
    },
    persist: true,

});